package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    /*需求:1.根据用户名和密码查询数据库
    * 2.将密码进行加密处理,md5加密方式
    * 3.用户名和密码正确,返回token
    * 4.独一无二的字符串 UUID 每次生成的几乎不重复
    * a09d973f-f4fa-11eb-8dd1-14dda954fcd5*/
    @Override
    public String login(User user) {
        /*1.将密码加密*/
        String password =user.getPassword();
        /*2.将字符串转化为字节数组*/
        byte[] passByte = password.getBytes();
        String md5Pass = DigestUtils.md5DigestAsHex(passByte);
        user.setPassword(md5Pass);
        QueryWrapper<User> queryWrapper =new QueryWrapper<>(user);

        User userDB = userMapper.selectOne(queryWrapper);
        if(userDB == null){
            return null;
        }
        //如果程序执行到这一行,说明输入正确,应该返回token
        String token = UUID.randomUUID().toString().replace("-", "");
        return token;
    }
    /*select * from user limit (n-1)*pageSize,pageSize*/
    /*@Override
    public PageResult getUserList(PageResult pageResult) {
        int start = (pageResult.getPageNum() - 1) * pageResult.getPageSize();
        int size = pageResult.getPageSize();
        List<User> userListByPage = userMapper.findUserListByPage(start, size);
        long total = userMapper.selectCount(null);
        return pageResult.setTotal(total).setRows(userListByPage);
    }*/
    public PageResult getUserList(PageResult pageResult){
        Page<User> page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"username",pageResult.getQuery());
        page = userMapper.selectPage(page, queryWrapper);
        List<User> userList = page.getRecords();
        long total = page.getTotal();
        pageResult.setTotal(total).setRows(userList);
        return pageResult;
    }

    @Override
    public void updateStatus(User user) {
        userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void addUser(User user) {
        byte[] bytes = user.getPassword().getBytes();
        String md5pass = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5pass);
        user.setStatus(true);
       /* Date date = new Date();
        user.setCreated(date).setUpdated(date);*/
        userMapper.insert(user);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User getById(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        User user1 = userMapper.selectOne(queryWrapper);
        return user1;
    }
    /*关于Spring中的事务策略说明
    0.Spring内部有事务的管理器,默认开启
    1.Spring只负责拦截运行时异常
     异常类型:1.运行时异常 2.检查异常/编译异常 由程序员自己控制
    2.属性1:rollbackFor 可以配置特殊的异常类型,遇到某种异常回滚
      属性2:noRollbackFor 可以配置异常类型,遇到某种异常不会滚
    3.既可以放在方法上,也可以放在类上,但一般是放在方法上,因为只有增删改需要加事务注解
    * */
    @Override
    @Transactional
    public void deleteUser(Integer id) {

        userMapper.deleteById(id);

    }
}
